#include <stdio.h>
#include "arch/pgd.h"
#include "kernel/time.h"

int map_page(uint* pgdt, uint virt_addr, struct page *p, uint flags)
{
	return 0;
}

int page_test(void);
int vma_test(void);
int chunk_test(void);
int slab_test(void);
int proc_test(void);
int vfs_test(void);

const static uint days_per_month_tbl[] = {
	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

static void sys_time_sec_inc(void)
{
	global_time.second = (global_time.second + 1) % TIME_MAX_SEC;
	if (!global_time.second)
	{
		global_time.minute = (global_time.minute + 1) % TIME_MAX_MIN;
		if (!global_time.minute)
		{
			global_time.hour = (global_time.hour + 1) % TIME_MAX_HOUR;
			if (!global_time.hour)
			{
				uint days_per_month = days_per_month_tbl[global_time.month - 1];
				if (IS_LEAP_YEAR(global_time.year) && global_time.month == MON_FEB)
					++days_per_month;

				if (++global_time.day > days_per_month)
				{
					global_time.day = 1;
					if (++global_time.month > TIME_MAX_MON)
					{
						global_time.month = 1;
						global_time.year++;
					}
				}
			}
		}
	}
}

static void print_time(void)
{
	printf("time: %u %u/%u %u:%02u:%02u\n",
		global_time.year, global_time.month, global_time.day,
		global_time.hour, global_time.minute, global_time.second);
}

static void main_test_time(void)
{
	times_t t;
	t.second = 59;
	t.minute = 1;
	t.hour = 21;
	t.day = 20;
	t.month = 3;
	t.year = 2005;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.minute = 59;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.hour = 23;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.day = 31;
	global_time = t;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.day = 29;
	t.month = 4;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.day = 30;
	t.month = 4;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.day = 28;
	t.month = 2;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.year = 1200;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.day = 29;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.day = 31;
	t.month = 12;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();

	t.day = 30;
	t.month = 9;
	global_time = t;
	print_time();

	sys_time_sec_inc();
	print_time();
}

int main(int argc, char *argv[])
{
	page_test();
	vma_test();
	chunk_test();
	
	main_test_time();

	slab_test();
	proc_test();
	vfs_test();
}